import _http from '@/utils/request'
import type {
  TradeMarkResponseData,
  AttributeCateResponseData,
  Attr,
  AttributeListResponseData,
  SpuListResponseData,
} from '@/api/product/type'
// 统一管理接口
enum API {
  TRADEMARK_URL = '/product/trademark/cate',
  ADDTRADEMARK_URL = '/product/trademark/save',
  UPDATETRADEMARK_URL = '/product/trademark/update',
  DELET_URL = '/product/trademark/remove',

  ATTRIBUTECATE_URL = '/product/attribute/cate',
  ATTRIBUTE_URL = '/product/attribute/list',
  ADDATTRIBUTE_URL = '/product/attribute/save',
  UPDATEATTRIBUTE_URL = '/product/attribute/update',
  DELETATTRIBUTE_URL = '/product/attribute/remove',

  SPU_LIST_URL = '/product/spu/list',
  ADD_SPU_URL = '/product/spu/add',
  UPDATE_SPU_URL = '/product/spu/update',
  DELET_SPU_URL = '/product/spu/remove',

  ALL_TRADEMARK_URL = '/product/spu/trademark/list',
  ALL_SALEATTR_URL = '/product/spu/saleattr/list',
  ID_SPUIMGLIST_URL = '/product/spu/id/imagelist',
  ID_SPUATTRLIST_URL = '/product/spu/id/attrlist',

  ID_SKULIST_URL = '/product/spu/id/skulist',
}
//请求品牌列表
export const resHasTrademark = (params) =>
  _http.get<any, TradeMarkResponseData>(API.TRADEMARK_URL, { params })
//添加和修改品牌
export const reqAddOrUpdateTrademark = (params: any) => {
  if (params.id) {
    //update
    return _http.post<any, any>(API.ADDTRADEMARK_URL, params)
  } else {
    //add
    return _http.post<any, any>(API.UPDATETRADEMARK_URL, params)
  }
}
//删除品牌列表
export const reqDeletTrademark = (id: number | string) =>
  _http.post<any, any>(API.DELET_URL, { id })

// -----------------------------------------
// 请求三级分类下拉选择数据
export const resHasAttributeCate = ({ level = 1, id = 0 } = {}) =>
  _http.get<any, AttributeCateResponseData>(API.ATTRIBUTECATE_URL, {
    params: { level, id },
  })

//请求属性列表
export const resHasAttribute = (params) =>
  _http.get<any, AttributeListResponseData>(API.ATTRIBUTE_URL, { params })
//添加和修改属性
export const reqAddOrUpdateAttribute = (params: Attr) => {
  if (params.id) {
    //update
    return _http.post<any, any>(API.ADDATTRIBUTE_URL, params)
  } else {
    //add
    return _http.post<any, any>(API.UPDATEATTRIBUTE_URL, params)
  }
}
//删除属性列表
export const reqDeletAttribute = (id: number) =>
  _http.post<any, any>(API.DELETATTRIBUTE_URL, { id })

// -----------------------------------------

//请求spu列表
export const resSpuList = (params) =>
  _http.get<any, SpuListResponseData>(API.SPU_LIST_URL, { params })
//添加和修改spu
export const reqAddOrUpdateSPU = (params: any) => {
  if (params.id) {
    //update
    return _http.post<any, any>(API.ADD_SPU_URL, params)
  } else {
    //add
    return _http.post<any, any>(API.UPDATE_SPU_URL, params)
  }
}
//删除spu列表
export const reqDeletSPU = (id: number) =>
  _http.post<any, any>(API.DELET_SPU_URL, { id })

export const resAllTrademark = (id) =>
  _http.get<any, any>(API.ALL_TRADEMARK_URL, { params: { id } })

export const resAllSaleAttr = (id) =>
  _http.get<any, any>(API.ALL_SALEATTR_URL, { params: { id } })

export const resIdBySpuImgList = (id) =>
  _http.get<any, any>(API.ID_SPUIMGLIST_URL, { params: { id } })

export const resIdBySpuAttrList = (id) =>
  _http.get<any, any>(API.ID_SPUATTRLIST_URL, { params: { id } })

export const resIdBySkuList = (id) =>
  _http.get<any, any>(API.ID_SKULIST_URL, { params: { id } })
